<script setup>
import { ref, onMounted } from 'vue'
import { tagDelService, tagGetService } from '@/api/tag'
import { formatTime } from '@/utils/format.js'

import tagEdit from './components/tagEdit.vue'

const tagList = ref([])
const dialog = ref()
const dialogVisible = ref(false)
// const tagOptions = ref([])
const params = ref({
  pagenum: 1,
  // pagesize: 5,
  id: '',
  tagName: ''
})
const loading = ref(false)

const getTagList = async () => {
  loading.value = true

  const res = await tagGetService(params.value)
  console.log(res.data)
  tagList.value = res.data.data

  loading.value = false
}
getTagList()

const onEdit = (row) => {
  console.log('Edit article', row)
  dialog.value.open(row)
  // 实现编辑逻辑
}

const onDelete = async (row) => {
  let id = row.id
  console.log('Delete article', id)
  await ElMessageBox.confirm('是否要删除', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  if (!Array.isArray(id)) {
    id = [id]
  }

  await tagDelService(id)
  ElMessage.success('删除成功')
  getTagList()
  // 实现删除逻辑
}

//添加逻辑
const onAdd = () => {
  dialogVisible.value = true
  dialog.value.open({})
}
const onSuccess = () => {
  getTagList()
}
onMounted(getTagList)
</script>

<!-- <template>
  <page-container title="类别管理">
    <template #extra>
      <el-button type="primary" @click="onAdd">发布类别</el-button>
    </template> -->

<!-- <el-form inline @submit.prevent="handleSearch">
      <el-form-item label="标签名称：" label-width="100"> </el-form-item>
      <el-form-item label-width="100">
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form> -->
<!-- 表格 -->
<!-- <el-table :data="tagList" style="width: 100%" v-loading="loading">
      <el-table-column label="序号" width="350" type="index"></el-table-column>

      <el-table-column
        label="标签名称"
        prop="tagName"
        width="430" 
      ></el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="480">
        <template #default="{ row }">
          {{ formatTime(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEdit(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelete(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <tag-edit ref="dialog" @success="onSuccess"></tag-edit>
  </page-container>
</template> -->

<template>
  <page-container title="类别管理">
    <template #extra>
      <el-button type="primary" @click="onAdd">发布类别</el-button>
    </template>
    <div class="card-container">
      <el-card
        class="box-card"
        v-for="item in tagList"
        :key="item.id"
        style="width: 300px; height: 220px"
      >
        <template v-slot:header>
          <div class="clearfix">
            <el-button
              style="float: right; padding: 0px 3px"
              type="text"
              @click="onEdit(item)"
              >编辑</el-button
            >
            <el-button
              style="float: right; padding: 0px 3px"
              type="text"
              @click="onDelete(item)"
              >删除</el-button
            >
          </div>
        </template>
        <div class="text item">
          {{ item.tagName }}
        </div>
        <div class="text time">{{ formatTime(item.createTime) }}</div>
      </el-card>
    </div>
    <tagEdit ref="dialog" @success="onSuccess"></tagEdit>
  </page-container>
</template>

<style scoped lang="scss">
.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 25px;
  margin-left: 25px;
}

.box-card {
  width: calc(20% - 15px); /* 25%的宽度减去左右各7.5px的间距 */
  margin: 25px;
  flex-shrink: 0; /* 防止卡片缩小 */
}

.text {
  font-size: 20px;
  margin-top: 10px;
}

.item {
  margin-bottom: 20px;
  font-weight: bold;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.time {
  margin-top: 30px;
  font-size: 16px;
}
.el-button {
  font-size: 16px;
}
</style>
